package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐控制类
 *
 * @author wc.L
 * @date 2022/4/26 15:51
 */
@Slf4j
@RequestMapping("/setmeal")
@RestController
public class SetmealCtroller {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CacheManager cacheManager;
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();
        LambdaQueryWrapper<Setmeal> setmealWrapper = new LambdaQueryWrapper<>();
        setmealWrapper.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);
        setmealWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(setmealPage, setmealWrapper);
        BeanUtils.copyProperties(setmealPage, setmealDtoPage, "setmealDishes");
        List<Setmeal> setmealList = setmealPage.getRecords();
        List<SetmealDto> setmealDtoList = setmealList.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            Category category = categoryService.getById(item.getCategoryId());
            if (item != null) {
                setmealDto.setCategoryName(category.getName());
            }
            BeanUtils.copyProperties(item, setmealDto);
            return setmealDto;
        }).collect(Collectors.toList());

        setmealDtoPage.setRecords(setmealDtoList);

        return R.success(setmealDtoPage);
    }
    @CacheEvict(value = "setmealCache",allEntries = true) //allEntries属性表示删除 setmealCache分类下边的所有的数据
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        setmealService.savwSetmealWithDish(setmealDto);
        return R.success("添加套餐成功");
    }

    /**
     * 修改时获取原属性信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getById(@PathVariable Long id) {
        Setmeal setmeal = setmealService.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealList = setmealDishService.list(queryWrapper);
        setmealDto.setSetmealDishes(setmealList);
        return R.success(setmealDto);
    }
    @CacheEvict(value = "setmealCache",allEntries = true) //allEntries属性表示删除 setmealCache分类下边的所有的数据
    @PutMapping
    public R<String> updateInfo(@RequestBody SetmealDto setmealDto) {
        //先修改菜品表
        setmealService.updateInfo(setmealDto);
        return R.success("修改成功");
    }

    /**
     * 单个删除或者批量删除菜品
     *
     * @param ids
     * @return
     */
    @CacheEvict(value = "setmealCache",allEntries = true) //allEntries属性表示删除 setmealCache分类下边的所有的数据
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids) {
        setmealService.delWithFlavorByID(ids);
        return R.success("删除成功");
    }

    /**
     * 停售操作
     *
     * @param ids
     * @return
     */
    @PostMapping("/status/0")
    public R<String> update0(@RequestParam List<Long> ids) {
        for (Long id : ids) {
            Setmeal setmeal = setmealService.getById(id);
            setmeal.setStatus(0);
            setmealService.updateById(setmeal);
        }
        return R.success("停售操作成功");
    }

    /**
     * 启售操作
     *
     * @param ids
     * @return
     */
    @PostMapping("/status/1")
    public R<String> update1(@RequestParam List<Long> ids) {
        for (Long id : ids) {
            Setmeal setmeal = setmealService.getById(id);
            setmeal.setStatus(1);
            setmealService.updateById(setmeal);
        }
        return R.success("启售操作成功");
    }

    //套餐相关搜索菜品接口
    @GetMapping("/list")
    @Cacheable(value = "setmealCache", key = "#setmeal.categoryId + '_' + #setmeal.status")
    public R<List<Setmeal>> list(Setmeal setmeal) {

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);

        return R.success(list);
    }
}
